package cc.mycraft.events;

import java.util.Date;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Beacon;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;

import cc.mycraft.MYCPlugin;

public final class MyCraftResourceBeaconDropEvent extends Event implements Cancellable {
	
	private static final HandlerList handlers = new HandlerList();

	@Override
	public HandlerList getHandlers() {
		return handlers;
	}
	
	public static HandlerList getHandlerList() {
		return handlers;
	}
	
	private MYCPlugin Plugin;
	private long Time;
	
	private Player bplayer;
	private Beacon beacon;
	private int exp;
	private Material[] Drops;
	private int DropAmount;
	private int DropGroup;
	private boolean cancel;
	
	public MyCraftResourceBeaconDropEvent(MYCPlugin plugin,Player player,Beacon b,Material[] items,int amount) {
		this.Plugin = plugin;
		this.Time = new Date().getTime();
		this.bplayer = player;
		this.beacon = b;
		this.Drops = items;
		this.exp=100;
		this.cancel = false;
		this.setAmount(amount);
	}
	
	public MYCPlugin getPluginInt() {
		return this.Plugin;
	}
	public long getTime() {
		return this.Time;
	}
	
	public String getPlayerName() {
		return this.bplayer.getName();
	}
	public Player getPlayer() {
		return this.bplayer;
	}
	public Beacon getBeacon() {
		return this.beacon;
	}
	public int getDropAmount() {
		return this.DropAmount;
	}
	public Material[] getDrops() {
		return this.Drops;
	}
	public void setExp(int Amount) {
		this.exp=Amount;
	}
	public void setAmount(int Amount) {
		if(Amount<1) {
			this.DropAmount=1;
			this.DropGroup=0;
			return;
		}
		if(Amount>64) {
			this.DropGroup = Amount/64;
			this.DropAmount = Amount - this.DropGroup*64;
		} else {
			this.DropAmount = Amount;
			this.DropGroup = 0;
		}
	}
	public void DropItem() {
		Location l = this.beacon.getLocation().add(0.5,1,0.5);
		if(this.DropAmount<=64) {
			for(Material m:this.Drops) {
				for(int i=0;i<this.DropGroup;i++) {
					l.getWorld().dropItem(l, new ItemStack(m,64));
				}
				l.getWorld().dropItem(l, new ItemStack(m,this.DropAmount));
			}
		} else {
			for(Material m:this.Drops) {
				l.getWorld().dropItem(l, new ItemStack(m,this.DropAmount));
			}
		}
		l.getWorld().playSound(l, Sound.ENTITY_CHICKEN_EGG, 1f, 1f);
		ExperienceOrb eo = (ExperienceOrb) l.getWorld().spawnEntity(l, EntityType.EXPERIENCE_ORB);
		eo.setExperience(this.exp);
	}
	public String getDropStr() {
		String str = this.Drops[0].toString();
		if(this.Drops.length<2) {
			return str;
		}
		str = "("+str;
		for(int i=1;i<this.Drops.length;i++) {
			str = str+","+this.Drops[i].toString();
		}
		str = str+")";
		return str;
	}

	@Override
	public boolean isCancelled() {
		return this.cancel;
	}

	@Override
	public void setCancelled(boolean arg0) {
		this.cancel = arg0;
	}
	
	
	
	
	
	
	
}
